//
//  ZYXPCLoginViewController.m
//  OCDataStore
//
//  登录页面
//

#import "ZYXPCLoginViewController.h"
#import "MBProgressHUD+MJ.h"

#define ZYXPCAccountKey         @"account"              //用户名
#define ZYXPCPwdKey             @"pwd"                  //密码
#define ZYXPCRmbPwdKey          @"rmb_pwd"              //是否记住密码
#define ZYXPCAutoLoginKey       @"auto_login"           //是否自动登录


@interface ZYXPCLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField    *accountField;
@property (weak, nonatomic) IBOutlet UITextField    *pwdField;
@property (weak, nonatomic) IBOutlet UIButton       *loginBtn;
@property (weak, nonatomic) IBOutlet UISwitch       *rmbPwdSwitch;
@property (weak, nonatomic) IBOutlet UISwitch       *autoLoginSwitch;


- (IBAction)rmbPwdChange;
- (IBAction)autoLoginChange;
- (IBAction)login;

@end

@implementation ZYXPCLoginViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.accountField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.pwdField];
    
    
    // 读取上次的配置
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.accountField.text   = [defaults objectForKey:ZYXPCAccountKey]; //用户名
    self.rmbPwdSwitch.on     = [defaults boolForKey:ZYXPCRmbPwdKey];    //是否记住密码
    self.autoLoginSwitch.on  = [defaults boolForKey:ZYXPCAutoLoginKey]; //是否自动登录
    
    // 处理密码
    if (self.rmbPwdSwitch.isOn) {
        self.pwdField.text   = [defaults objectForKey:ZYXPCPwdKey];
    }
    
    // 处理自动登录的情况
    if (self.autoLoginSwitch.isOn) {
        [self login];                                                   // 点击了"登录"按钮
    }
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


/**
 *  文本框的文字发生改变的时候调用
 */
- (void)textChange
{
    // 控制按钮的状态
    //    if (self.accountField.text.length && self.pwdField.text.length) {
    //        self.loginBtn.enabled = YES;
    //    } else {
    //        self.loginBtn.enabled = NO;
    //    }
    self.loginBtn.enabled = (self.accountField.text.length && self.pwdField.text.length);
}


/**
 *  记住密码开关的状态改变就会调用
 */
- (IBAction)rmbPwdChange {
    // 取消了记住密码
    if (self.rmbPwdSwitch.isOn == NO) {
        //self.autoLoginSwitch.on = NO;
        [self.autoLoginSwitch setOn:NO animated:YES];
    }
}


/**
 *  自动登录的状态改变就会调用
 */
- (IBAction)autoLoginChange {
    if (self.autoLoginSwitch.isOn) {
        //self.rmbPwdSwitch.on = YES;
        [self.rmbPwdSwitch setOn:YES animated:YES];
    }
}


/**
 *  登录
 */
- (IBAction)login { // 帐号:solozyx  密码:123
    if (![self.accountField.text isEqualToString:@"solozyx"]) {
        [MBProgressHUD showError:@"帐号不存在"];
        return;
    }
    
    if (![self.pwdField.text isEqualToString:@"123"]) {
        [MBProgressHUD showError:@"密码错误"];
        return;
    }
    
    // 显示一个蒙版(遮盖)
    [MBProgressHUD showMessage:@"正在登录中...."];
    
    // 发送网络请求
    
    // 模拟(2秒后执行跳转)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 移除遮盖
        [MBProgressHUD hideHUD];
        
        // 跳转 -- 执行login2contacts这个segue
        [self performSegueWithIdentifier:@"login2contacts" sender:nil];
        
        
        // 存储数据
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:self.accountField.text          forKey:ZYXPCAccountKey];
        [defaults setObject:self.pwdField.text              forKey:ZYXPCPwdKey];
        [defaults setBool:self.rmbPwdSwitch.isOn            forKey:ZYXPCRmbPwdKey];
        [defaults setBool:self.autoLoginSwitch.isOn         forKey:ZYXPCAutoLoginKey];
        
        //注意:UserDefaults设置数据时，不是立即写入，而是根据时间戳定时地把缓存中的数据写入本地磁盘。
        //所以调用了set方法之后数据有可能还没有写入磁盘应用程序就终止了。
        //出现以上问题，可以通过调用synchornize方法强制写入
        [defaults synchronize];
        
        

        //获得Documents的全路径
        //NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        //ZYXLog(@"%@",doc);
        
        // 2015-04-04 22:35:28.908 OCDataStore[33956:746275]
        // /Users/zhaoyingxin/Library/Developer/CoreSimulator/Devices/E92F8A62-B239-4ADA-945B-D8028C94FD5C/data/Containers/Data/Application/6EFF3093-1062-45EA-84EE-8589D0199498/Documents
        
        NSString *doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        ZYXLog(@"%@",doc);
        
        // 2015-04-04 22:41:44.254 OCDataStore[34414:759551]
        // /Users/zhaoyingxin/Library/Developer/CoreSimulator/Devices/E92F8A62-B239-4ADA-945B-D8028C94FD5C/data/Containers/Data/Application/688FC2ED-6074-40F5-AE20-9E3ABE229FB9/Library/Caches
        
    });
}


/**
 *  执行segue后,跳转之前会调用这个方法
 *  一般在这里给下一个控制器传递数据
 */
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // 1.取得目标控制器(联系人列表控制器)
    UIViewController *contactVc = segue.destinationViewController;
    
    // 2.设置标题
    contactVc.title = [NSString stringWithFormat:@"%@的联系人列表", self.accountField.text];
    // contactVc.title 等价于 contactVc.navigationItem.title
    // contactVc.navigationItem.title = [NSString stringWithFormat:@"%@的联系人列表", self.accountField.text];
}


@end
